package com.mes.web.controller.biz;

import com.github.pagehelper.PageInfo;
import com.mes.biz.domain.BizWarehouse;
import com.mes.biz.domain.BizWarehouseSpace;
import com.mes.biz.service.BizWarehouseSpaceService;
import com.mes.common.annotation.Log;
import com.mes.common.core.controller.BaseController;
import com.mes.common.core.domain.R;
import com.mes.common.core.page.PageDomain;
import com.mes.common.enums.BusinessType;
import com.mes.common.utils.SecurityUtils;
import com.mes.common.utils.poi.ExcelUtil;
import com.mes.framework.cache.DictUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 仓位管理Controller
 * @author hwg
 * @email hwg
 * @date 2023-04-09
 */
@RestController
@RequestMapping("/biz/bizWarehouseSpace")
public class BizWarehouseSpaceController extends BaseController {

    @Autowired
    private BizWarehouseSpaceService bizWarehouseSpaceService;

    /**
     * 查询仓位管理列表
     */
    @PreAuthorize("@ss.hasPermi('biz:bizWarehouseSpace:list')")
    @GetMapping("/list")
    public R<PageInfo> list(BizWarehouseSpace bizWarehouseSpace, HttpServletRequest request, HttpServletResponse response) {
        bizWarehouseSpace.setPage(new PageDomain(request, response));
        return R.data(bizWarehouseSpaceService.findPage(bizWarehouseSpace));
    }

    @GetMapping("/appletList")
    public R<List<BizWarehouseSpace>> appletList(BizWarehouseSpace bizWarehouseSpace, HttpServletRequest request, HttpServletResponse response) {
        return R.data(bizWarehouseSpaceService.findList(bizWarehouseSpace));
    }

    /**
     * 查询仓位管理列表
     */
    @PreAuthorize("@ss.hasPermi('biz:bizWarehouseSpace:list')")
    @GetMapping("/listByWarehouseId/{warehouseId}")
    public R<List<BizWarehouseSpace>> listByWarehouseId(@PathVariable("warehouseId") String warehouseId) {
        BizWarehouseSpace bizWarehouseSpace = new BizWarehouseSpace();
        bizWarehouseSpace.setWarehouseId(warehouseId);
        return R.data(bizWarehouseSpaceService.findList(bizWarehouseSpace));
    }

    /**
     * 获取仓位管理详细信息
     */
    @PreAuthorize("@ss.hasPermi('biz:bizWarehouseSpace:query')")
    @GetMapping(value = "/{id}")
    public R<BizWarehouseSpace> detail(@PathVariable("id") String id) {
        return R.data(bizWarehouseSpaceService.get(id));
    }

    /**
     * 新增仓位管理
     */
    @PreAuthorize("@ss.hasPermi('biz:bizWarehouseSpace:add')")
    @Log(title = "仓位管理", businessType = BusinessType.INSERT)
    @PostMapping
    public R add(@RequestBody @Validated  BizWarehouseSpace bizWarehouseSpace) {
        bizWarehouseSpace.setTenantId(SecurityUtils.getTenantId());
        return R.status(bizWarehouseSpaceService.save(bizWarehouseSpace));
    }

    /**
     * 修改仓位管理
     */
    @PreAuthorize("@ss.hasPermi('biz:bizWarehouseSpace:edit')")
    @Log(title = "仓位管理", businessType = BusinessType.UPDATE)
    @PutMapping
    public R edit(@RequestBody @Validated BizWarehouseSpace bizWarehouseSpace) {
        return R.status(bizWarehouseSpaceService.save(bizWarehouseSpace));
    }

    /**
     * 删除仓位管理
     */
    @PreAuthorize("@ss.hasPermi('biz:bizWarehouseSpace:remove')")
    @Log(title = "仓位管理", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public R remove(@PathVariable String[] ids) {
        return R.status(bizWarehouseSpaceService.deleteBizWarehouseSpaceByIds(ids));
    }


    /**
     * 导出仓位管理列表
     */
    @PreAuthorize("@ss.hasPermi('biz:bizWarehouseSpace:export')")
    @Log(title = "仓位管理", businessType = BusinessType.EXPORT)
    @GetMapping("/export")
    public R export(BizWarehouseSpace bizWarehouseSpace) {
        List<BizWarehouseSpace> list = bizWarehouseSpaceService.findList(bizWarehouseSpace);
        ExcelUtil<BizWarehouseSpace> util = new ExcelUtil<BizWarehouseSpace>(BizWarehouseSpace.class);
        return util.exportExcel(list, "仓位管理数据");
    }

    /**
     * 根据字典类型查询字典数据信息等其他自定义信息
     */
    @GetMapping(value = "/getInitData/{dictTypes}")
    public R getInitData(@PathVariable String dictTypes) {
        Map<String, Object> dataMap = new HashMap<String, Object>();
        dataMap.putAll(DictUtils.getMultiDictList(dictTypes));
        return R.data(dataMap);
    }

    @GetMapping("/spaceList/{warehouseId}/{productId}")
    public R<List<BizWarehouseSpace>> spaceList(@PathVariable("warehouseId") String warehouseId,@PathVariable("productId") String productId) {
        return R.data(bizWarehouseSpaceService.spaceList(warehouseId,productId));
    }

}
